﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

namespace TenSevenNine.Web.Templates.ProjectItem
{
    public partial class _default : Acms.AcmsControls.TemplatePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            dsChildren.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsChildren_SelectData);



        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();
                
                var post = AcmsContext.CurrentPost;
                if (post != null)
                {
                    //var lnkWebsite = FindControl("lnkWebsite") as HyperLink;
                    if (lnkWebsite != null)
                    {
                        var website = post.FindPostControl("Link");
                        //var websiteDisplayName = post.FindPostControl("WebsiteDisplayName");
                        if (website != null)
                        {
                            var websiteProp = website.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
                            //var websiteDisplayNameProp = websiteDisplayName.GetProperties<Acms.AcmsControls.TextContentControlProperties>();

                            if (websiteProp != null)
                            {
                                String propUrl = websiteProp.Text;
                                lnkWebsite.Text = websiteProp.Text;
                                String httpTest = propUrl.Substring(0, 3);
                                bool addHttp = String.Compare(propUrl.Substring(0, 3), "www") == 0; // This is true.
                                if (addHttp == true)
                                {
                                    propUrl = "http://" + propUrl;
                                }
                                lnkWebsite.NavigateUrl = propUrl;
                                lnkWebsite.Visible = true;
                            }
                            else
                            {
                                lnkWebsite.Visible = false;
                            }
                        }
                    }
                }
            }
        }

        public override void DataBind()
        {
            base.DataBind();
        }

        void dsChildren_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
        {
            e.SetData(Post.GetAllBySiteIdAndParentPath(AcmsContext.SiteId, AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion));
        }


    }
}